//
//  LoopScrollView.m
//  LoopScrollView
//
//  Created by cardioray on 2017/4/17.
//  Copyright © 2017年 kinglong. All rights reserved.
//

#import "LoopScrollView.h"

#define TOTAL_SIZE_PER_DISPLAY 3

@interface LoopScrollView () <UIScrollViewDelegate>

//config
@property (nonatomic, strong) NSArray * mImages;
@property (nonatomic, strong) NSArray * mMessages;

@property (nonatomic, strong) UIImage * mLoadingPlaceholder;
@property (nonatomic, strong) UIImage * mNotFoundPlaceholder;
@property (nonatomic, assign) BOOL      mEnablePageControl;

//view builder
@property (nonatomic, strong) UIScrollView * mMainScrollView;

//view paramater
@property (nonatomic, assign) CGFloat   mWidth;
@property (nonatomic, assign) CGFloat   mHeight;
@property (nonatomic, assign) NSInteger mCurrentIndex;

@end

@implementation LoopScrollView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self initConfig];
        [self initSubviews];
    }
    return self;
}

- (void)initSubviews {
    _mMainScrollView = [[UIScrollView alloc]initWithFrame:CGRectZero];
    _mMainScrollView.pagingEnabled = YES;
    _mMainScrollView.showsHorizontalScrollIndicator = NO;
    _mMainScrollView.delegate = self;
    [self addSubview:_mMainScrollView];
}

- (void)initConfig {
    _mWidth = 0;
    _mHeight = 0;
    _mCurrentIndex = 0;
}

#pragma mark user set config
- (void)setImages:(NSArray *)images {
    _mImages = images;
}

- (void)setMessages:(NSArray *)messages {
    _mMessages = messages;
}

- (void)setLoadingPlaceholder:(UIImage *)loadingPlaceholder {
    _mLoadingPlaceholder = loadingPlaceholder;
}

- (void)setNotFoundPlaceholder:(UIImage *)notFoundPlaceholder {
    _mNotFoundPlaceholder = notFoundPlaceholder;
}

- (void)setPageControlEnable:(BOOL)enable {
    _mEnablePageControl = enable;
}

//get view frame when autolayout changed
- (void)layoutSubviews {
    
    if (_mWidth != 0 && _mHeight != 0) {
        NSLog(@"Has initialize by os.");
        return;
    }
    
    if (_mImages == nil || [_mImages count] <= 0) {
        return;
    }

    _mWidth = self.frame.size.width;
    _mHeight = self.frame.size.height;
    
    [_mMainScrollView setFrame:CGRectMake(0, 0, _mWidth, _mHeight)];
    [_mMainScrollView setContentSize:CGSizeMake(TOTAL_SIZE_PER_DISPLAY * _mWidth, _mHeight)];
    
    [self reloadScrollView];
}

- (void)reloadScrollView {
    
    for (UIView * view in [_mMainScrollView subviews]) {
        [view removeFromSuperview];
    }
    
    NSArray * currentImages = [self getCurrentImages:_mCurrentIndex];
    
    for (int i = 0; i < [currentImages count]; i++) {
        
        id item = [currentImages objectAtIndex:i];
        if ([item isKindOfClass:[UIImage class]]) {
            UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(_mWidth * i, 0, _mWidth, _mHeight)];
            imageView.layer.cornerRadius = 5.0f;
            imageView.layer.borderWidth = 1.0f;
            imageView.layer.borderColor = [UIColor lightGrayColor].CGColor;
            imageView.image = item;
            [_mMainScrollView addSubview:imageView];
        }
    }
    
    _mMainScrollView.contentOffset = CGPointMake(_mWidth, 0);
}

- (NSArray *)getCurrentImages:(NSInteger)index {
    return @[[_mImages objectAtIndex:[self getPreIndexByIndex:index]],
             [_mImages objectAtIndex:index],
             [_mImages objectAtIndex:[self getNextIndexByIndex:index]]];
}

- (NSInteger)getPreIndexByIndex:(NSInteger)index {
    NSInteger count = [_mImages count];
    if (index == 0) {
        return count - 1;
    } else {
        return index - 1;
    }
}

- (NSInteger)getNextIndexByIndex:(NSInteger)index {
    NSInteger count = [_mImages count];
    if (index == count - 1) {
        return 0;
    } else {
        return index + 1;
    }
}

#pragma mark UIScrollViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {

    CGPoint offset = _mMainScrollView.contentOffset;
    if (offset.x == _mWidth) {
        return;
    }
    
    if (offset.x < _mCurrentIndex) {
        _mCurrentIndex = [self getPreIndexByIndex:_mCurrentIndex];
    } else {
        _mCurrentIndex = [self getNextIndexByIndex:_mCurrentIndex];
    }

    [self reloadScrollView];
}

@end
